package com.jeeplus.test.jlnu.admissionInfo.controller;

import com.jeeplus.test.jlnu.graduationInfo.domain.entity.GraduationPameter;
import com.jeeplus.test.jlnu.graduationInfo.mapper.GraduationInfoMapper;
import com.jeeplus.test.jlnu.graduationInfo.service.GraduationInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.spring5.SpringTemplateEngine;
import org.thymeleaf.context.Context;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

@RestController
public class PdfController {

    @Autowired
    private GraduationInfoService graduationInfoService;
    private final SpringTemplateEngine templateEngine;

    public PdfController(SpringTemplateEngine templateEngine) {
        this.templateEngine = templateEngine;
    }

    @GetMapping("/generatePdf")
    @ResponseBody
    public void generatePdf(HttpServletResponse response, @RequestParam String name, @RequestParam String studentId) throws IOException {
        GraduationPameter graduationPameter1 = graduationInfoService.selectAllByStuId(studentId,name);
        String diplomaNum = graduationPameter1.getDiplomaNum();
        String degreeCertificateNum = graduationPameter1.getDegreeCertificateNum();
        response.setContentType("text/html; charset=UTF-8");
        // 模拟数据
        Map<String, Object> variables = new HashMap<>();
        if (graduationPameter1 != null) {
            // 组合 diplomaNum 和 degreeCertificateNum 生成 reportNumber
            String reportNumber = (diplomaNum != null ? diplomaNum : "") + "-" + (degreeCertificateNum != null ? degreeCertificateNum : "");
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            String queryDate = LocalDateTime.now().format(formatter);
            variables.put("queryDate", queryDate);
            variables.put("reportNumber", reportNumber);

            // 将 GraduationPameter 对象中的字段逐一放入 variables
            variables.put("idCard",graduationPameter1.getIdcardNum());
            variables.put("studentId", graduationPameter1.getStudentId());
            variables.put("admissionMajor", graduationPameter1.getAdmissionMajor());
            variables.put("name", graduationPameter1.getName());
            variables.put("gender", graduationPameter1.getGender());
            variables.put("nation", graduationPameter1.getNation());
            variables.put("nativePlace", graduationPameter1.getNativePlace());
            Date birthday = graduationPameter1.getBirthday();
            DateTimeFormatter formatterss = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            String formattedGraduationDates = birthday.toInstant()
                    .atZone(ZoneId.systemDefault())
                    .toLocalDateTime()
                    .format(formatter);
            variables.put("birthday", formattedGraduationDates);
            variables.put("studyStartTime", graduationPameter1.getStudyStartTime());
            variables.put("studyEndTime", graduationPameter1.getStudyEndTime());
            variables.put("school", graduationPameter1.getSchool());
            variables.put("educationalSystem", graduationPameter1.getEducationalSystem());
            variables.put("gradation", graduationPameter1.getGradation());

            if(graduationPameter1.getGraduationDate() == null){
                variables.put("graduationDate", "暂无");
            }else {
                Date graduationDate = graduationPameter1.getGraduationDate();
                DateTimeFormatter formatters = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
                String formattedGraduationDate = graduationDate.toInstant()
                        .atZone(ZoneId.systemDefault())
                        .toLocalDateTime()
                        .format(formatter);
                variables.put("graduationDate", formattedGraduationDate);
            }

            variables.put("diplomaNum", diplomaNum);
            variables.put("degreeLevel", graduationPameter1.getDegreeLevel());
            variables.put("degreeType", graduationPameter1.getDegreeType());
            variables.put("degreeCertificateNum", degreeCertificateNum);
            variables.put("imageUrl", "http://192.168.180.70:8990/jeeplus-vue/static/search.jpg");
            variables.put("verificationUrl", "https://www.jlnu.edu.cn/dag/");
        }


        String html = templateEngine.process("template", new Context(Locale.CHINA, variables));
// 写入响应
        response.getWriter().write(html);
        response.getWriter().flush();
    }

}